rm(list=ls())

pacman::p_load(tidyverse, glue, ggpubr)
setwd('put_your_wd_here')

files <- c('meta_google_all.csv',
           'meta_google_gun_control_orgs.csv',
           'meta_google_gun_rights_orgs.csv',
           'meta_twitter_all.csv',
           'meta_petitions_all.csv',
           'meta_donations_giffords.csv',
           'meta_donations_NRA.csv')
files <- glue::glue('datasets/metaanalysis/{files}')
out <- map(files, read_csv)

out[[1]] <- out[[1]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = gsub('Meta ','',term),
         platform = "Google Trends") %>%
  select(shooting, Coef, lower, upper, term,platform)

out[[2]] <- out[[2]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = "Gun Control Orgs",
         platform = "Google Trends") %>%
  select(shooting, Coef, lower, upper, term,platform)

out[[3]] <- out[[3]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = "Gun Rights Orgs",
         platform = "Google Trends") %>%
  select(shooting, Coef, lower, upper, term,platform)

out[[4]] <- out[[4]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = gsub('Meta ','',term),         
         platform = "Twitter") %>%
  filter(term != '#recycling') %>%
  select(shooting, Coef, lower, upper, term,platform)

out[[5]] <- out[[5]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = gsub(' Petition','',outcome),         
         platform = "Petitions") %>%
  select(shooting, Coef, lower, upper, term,platform)

out[[6]] <- out[[6]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = paste0(outcome, gsub('Meta ',' ',group_var), sep=''),         
         platform = "Donations") %>%
  select(shooting, Coef, lower, upper, term,platform)
out[[7]] <- out[[7]] %>%
  mutate(shooting = gsub('Meta Estimate |\\(|\\)','',shooting),
         term = paste0(outcome, gsub('Meta ',' ',group_var), sep=''),         
         platform = "Donations") %>%
  select(shooting, Coef, lower, upper, term,platform)

dat <- bind_rows(out) 

plot1 <- dat %>%
  filter(shooting == 'Top 10') %>%
  mutate(platform = factor(platform, levels=c(
    'Google Trends','Twitter','Petitions','Donations'
  ))) %>%
  mutate(statsig = factor(ifelse(
    (lower < 0 & upper < 0) |
      (lower > 0 & upper > 0), 1, 0)
  )) %>%
  mutate(term = factor(term, levels=rev(c(
    'Gun Control','Gun Rights','Gun Control Orgs','Gun Rights Orgs',
    '#guncontrol','#gunrights','#everytown','#NRA',
    'Giffords PAC Amount','Giffords PAC Number',
    'NRA PAC Amount','NRA PAC Number'
  )))) %>%
  ggplot(aes(x=term, y=Coef, ymin=lower, ymax=upper, color=statsig)) +
  geom_hline(yintercept=0, linetype=2, color='red') +
  geom_errorbar(width=0.1) +
  geom_point(shape=21, fill='white', size=3) +
  facet_wrap(~platform, ncol=1, scales='free_y') +
  coord_flip() +
  scale_color_manual(values=c('grey50','Black')) +
  labs(x='', y='',title='(A) Top 10') +
  theme_bw() +
  theme(legend.position = 'None') +
  theme(legend.position='none', 
        plot.title = element_text(hjust = 0.5)) +
  scale_y_continuous(limits=c(-1,3.32))

plot2 <- dat %>%
  filter(shooting == 'Top 20') %>%
  mutate(platform = factor(platform, levels=c(
    'Google Trends','Twitter','Petitions','Donations'
  ))) %>%
  mutate(statsig = factor(ifelse(
    (lower < 0 & upper < 0) |
      (lower > 0 & upper > 0), 1, 0)
  )) %>%
  mutate(term = factor(term, levels=rev(c(
    'Gun Control','Gun Rights','Gun Control Orgs','Gun Rights Orgs',
    '#guncontrol','#gunrights','#everytown','#NRA',
    'Giffords PAC Amount','Giffords PAC Number',
    'NRA PAC Amount','NRA PAC Number'
  )))) %>%
  ggplot(aes(x=term, y=Coef, ymin=lower, ymax=upper, color=statsig)) +
  geom_hline(yintercept=0, linetype=2, color='red') +
  geom_errorbar(width=0.1) +
  geom_point(shape=21, fill='white', size=3) +
  facet_wrap(~platform, ncol=1, scales='free_y') +
  coord_flip() +
  scale_color_manual(values=c('grey50','Black')) +
  labs(x='', y='',title='(B) Top 20') +
  theme_bw() +
  theme(legend.position = 'None') +
  theme(legend.position='none', 
        plot.title = element_text(hjust = 0.5),
        axis.text.y = element_blank()) +
  scale_y_continuous(limits=c(-1,3.32))

plot3 <- dat %>%
  filter(shooting == 'Bottom 20') %>%
  mutate(platform = factor(platform, levels=c(
    'Google Trends','Twitter','Petitions','Donations'
  ))) %>%
  mutate(statsig = factor(ifelse(
    (lower < 0 & upper < 0) |
      (lower > 0 & upper > 0), 1, 0)
  )) %>%
  mutate(term = factor(term, levels=rev(c(
    'Gun Control','Gun Rights','Gun Control Orgs','Gun Rights Orgs',
    '#guncontrol','#gunrights','#everytown','#NRA',
    'Giffords PAC Amount','Giffords PAC Number',
    'NRA PAC Amount','NRA PAC Number'
  )))) %>%
  ggplot(aes(x=term, y=Coef, ymin=lower, ymax=upper, color=statsig)) +
  geom_hline(yintercept=0, linetype=2, color='red') +
  geom_errorbar(width=0.1) +
  geom_point(shape=21, fill='white', size=3) +
  facet_wrap(~platform, ncol=1, scales='free_y') +
  coord_flip() +
  scale_color_manual(values=c('grey50','Black')) +
  labs(x='', y='',title='(C) Bottom 20') +
  theme_bw() +
  theme(legend.position = 'None') +
  theme(legend.position='none', 
        plot.title = element_text(hjust = 0.5),
        axis.text.y = element_blank()) +
  scale_y_continuous(limits=c(-1,3.32))

plots <- ggarrange(plot1, plot2,plot3,
                   heights = c(4,4,4),
                   widths = c(6.75,5,5),
                   ncol = 3, nrow = 1)
plots
plots <- annotate_figure(plots, bottom = text_grob("'Pooled Effects\n(Standard Deviations)'",family='Helvetica'))
ggsave(plots, width=10, height=5, 
       file='meta_all.png', bg='white')